package com.bailiban.search.service;

import com.bailiban.search.dao.ArticleDao;
import com.bailiban.search.entity.Article;
import org.apache.lucene.queryparser.flexible.core.builders.QueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhulang
 * @Classname ArticleService
 * @Description ArticleService
 */
@Service
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 添加索引
     *
     * @param article
     */
    public void add(Article article) {
        articleDao.save(article);
    }

    public Page search(int page, int size, String keywords) {
        PageRequest of = PageRequest.of(page - 1, size);
        return articleDao.findByTitleOrContentLike(keywords, keywords, of);
    }

    public Article findById(String id) {
        return articleDao.findById(id).get();
    }

    public void update(Article article) {
        articleDao.save(article);
    }

    public void delete(String id) {
        articleDao.deleteById(id);
    }

    public List<Article> getHight(String keywords) {
//        构建了一个查询对象
        BoolQueryBuilder should = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery("title", keywords))
                .should(QueryBuilders.matchQuery("content", keywords));
//        构建的查询高亮对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(should)
                .withHighlightFields(new HighlightBuilder
                                .Field("title")
                                .preTags("<span style='color:red'>")
                                .postTags("</span>"),
                        new HighlightBuilder
                                .Field("content")
                                .preTags("<span style='color:red'>")
                                .postTags("</span>"))
                .build();
//通过elasticsearchRestTemplate构建查询
        SearchHits<Article> searchHits = elasticsearchRestTemplate.search(build, Article.class);
//        构建一个空集合，用于添加要返回的article
        ArrayList<Article> articles = new ArrayList<>();

        for (SearchHit<Article> searchHit : searchHits) {
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            searchHit.getContent().setTitle(highlightFields.get("title") == null ? searchHit.getContent().getTitle() : highlightFields.get("title").get(0));
            searchHit.getContent().setContent(highlightFields.get("content") == null ? searchHit.getContent().getContent() : highlightFields.get("content").get(0));
            articles.add(searchHit.getContent());
        }
        return articles;
    }
}
